﻿Public Class frmCJModify
    Private Sub frmCJModify_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim icount As Integer
        Dim i As Integer
        cboZY.Items.Clear()
        cboCourse.Items.Clear()
        cboStudent.Items.Clear()
        '
        txtSQL = "SELECT 专业名称 FROM 专业"
        icount = ExecuteSQL(txtSQL, ErrorMsg)
        For i = 0 To icount - 1
            cboZY.Items.Add(DBSet.Tables.Item(0).Rows(i).Item("专业名称").ToString)
        Next i
        '
        txtSQL = "SELECT 课程编号,课程名称 FROM 课程"
        icount = ExecuteSQL(txtSQL, ErrorMsg)
        For i = 0 To icount - 1
            cboCourse.Items.Add(Trim(DBSet.Tables.Item(0).Rows(i).Item("课程编号").ToString) & "--" & DBSet.Tables.Item(0).Rows(i).Item("课程名称").ToString)
        Next i
        cboCourse.SelectedIndex = 0
    End Sub
    '专业变化时下拉框
    Private Sub cboZY_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboZY.SelectedIndexChanged
        Dim icount As Integer
        Dim i As Integer
        cboStudent.Items.Clear()
        If cboZY.Text = "" Then
            MsgBox("您还没有选择专业！")
            Exit Sub
        End If
        txtSQL = "SELECT 学号,姓名 FROM 学生" & " WHERE 专业名称='" & cboZY.Text & "'"
        icount = ExecuteSQL(txtSQL, ErrorMsg)
        For i = 0 To icount - 1
            cboStudent.Items.Add(DBSet.Tables.Item(0).Rows(i).Item("学号").ToString & "--" & DBSet.Tables.Item(0).Rows(i).Item("姓名").ToString)
        Next i
        cboStudent.SelectedIndex = -1
    End Sub
    '修改按钮
    Private Sub cmdModify_Click(sender As Object, e As EventArgs) Handles cmdModify.Click
        Dim xh As String
        Dim kz As String
        Dim icount As Integer
        If cboCourse.Text = " " Then
            MsgBox("您还没有选择课程！")
            Exit Sub
        End If
        If cboStudent.Text = "" Then
            MsgBox("您还没有选择学生学号！")
            Exit Sub
        End If
        '判断数值
        If Not IsNumeric(txtScore.Text) Then
            MsgBox（"分数输入不为数字！"）
            txtScore.Focus()
            Exit Sub
        End If
        '取课程编号
        kz = Microsoft.VisualBasic.Left(cboCourse.Text, InStr(1, cboCourse.Text, "-") - 1)
        xh = Microsoft.VisualBasic.Left(cboStudent.Text, InStr(1, cboStudent.Text, "-") - 1)
        txtSQL = "Select * FROM 选课 WHERE 学号='" & xh & " ' AND 课程编号='" & kz & "'"
        If icount = 1 Then
            txtSQL = "UPDATE 选课 SET 成绩=" & txtScore.Text & " WHERE 学号='" & xh & kz & "'AND 课程编号=' " & kz & " ' "
            ExecuteSQL(txtSQL, ErrorMsg)
            MsgBox("修改成功！")
            Exit Sub
        ElseIf icount = 0 Then
            txtSQL = "INSER INTO 选课(学号,课程编号,成绩) VALUES('" & xh & "','" & kz & "'," & txtScore.Text & ")"
            ExecuteSQL(txtSQL, ErrorMsg)
            MsgBox("写入成功！", vbOKOnly)
            txtScore.Text = ""
            txtScore.Focus()
        Else
            MsgBox("访问数据库出错!")
            Exit Sub
        End If
    End Sub
    '学生下拉列表框发生变化
    Private Sub cboStudent_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboStudent.SelectedIndexChanged
        Dim xh As String
        Dim kz As String
        Dim icount As Integer
        txtScore.Text = ""
        If cboCourse.Text = "" Then
            Exit Sub
        End If
        If cboStudent.Text = " " Then
            Exit Sub
        End If
        '
        kz = Microsoft.VisualBasic.Left(cboCourse.Text, InStr(1, cboCourse.Text, "-") - 1)
        xh = Microsoft.VisualBasic.Left(cboStudent.Text, InStr(1, cboStudent.Text, "-") - 1)
        txtSQL = "Select  成绩 FROM 选课 WHERE 学号='" & xh & " ' AND 课程编号='" & kz & "'"
        icount = ExecuteSQL(txtSQL, ErrorMsg)
        If icount = 1 Then
            txtScore.Text = DBSet.Tables.Item(0).Rows(0).Item("成绩")
        End If
    End Sub
    '删除按钮
    Private Sub cmdDelete_Click(sender As Object, e As EventArgs) Handles cmdDelete.Click
        Dim xh As String
        Dim kz As String
        Dim icount As Integer
        If cboCourse.Text = " " Then
            MsgBox("您还没有选择课程！")
            Exit Sub
        End If
        If cboStudent.Text = " " Then
            MsgBox("您还没有选择学号！")
            Exit Sub
        End If
        kz = Microsoft.VisualBasic.Left(cboCourse.Text, InStr(1, cboCourse.Text, "-") - 1)
        xh = Microsoft.VisualBasic.Left(cboStudent.Text, InStr(1, cboStudent.Text, "-") - 1)
        txtSQL = "Delete * FROM 选课 WHERE 学号='" & xh & " ' AND 课程编号='" & kz & "'"
        icount = ExecuteSQL(txtSQL, ErrorMsg)
        If icount > 0 Then
            MsgBox("删除成功！")
        Else
            MsgBox（"删除失败！"）
        End If
    End Sub

    Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub
End Class